/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



import es.uned.modelo.*;
import java.io.FileReader;

/**
 *
 * @author Gabriel Manzano Mérida
 */
public class Mapa {
    
    private char map[][];    
    private String rutaMapa = "../Pacman_v2/src/resources/map1.txt";
    private int pellets = 0;
    private int powerPellets = 0;
    
    public Mapa(){
        map = new char[PacmanUtils.COLUMNAS][PacmanUtils.FILAS]; 
        // lee caracteres de un fichero
        try {
            FileReader fr = new FileReader(this.rutaMapa);
               //Leer el primer carácter
            //Se debe almacenar en una variable de tipo int
            int caract = 0;
               //Se recorre el fichero hasta encontrar el carácter -1
            //   que marca el final del fichero

            int x = 0, y = 0;
            while (caract != -1) {
                caract = fr.read();
                
                //actualizamos estado
                if (caract=='2') this.powerPellets++;
                if (caract=='3') this.pellets++;
                
                //Leer el siguiente carácter
                x++;
                if (x < PacmanUtils.COLUMNAS) {
                    this.map[x][y] = (char) caract;
                    
                    System.out.print((char)caract);
                } else {
                    y++;
                    x = 0;
                    System.out.println();
                }
                 
            } 
            fr.close(); 
                
        } catch (Exception e) {
            System.out.println("Error de lectura del fichero: " + e.toString());
        }
    }

    @Override
    public String toString() {
        String mapa = new String();
        char c;

        for (int x = 0; x < PacmanUtils.COLUMNAS; x++) {
            for (int y = 0; y < PacmanUtils.FILAS; y++) {
                c = map[x][y];
                mapa += c;
            }
            mapa += "\n";
        }
        return mapa;
    }
    
    public char getElement(int x, int y){
        char c = '1';
        if (this.map!=null && checkBorders(x,y)) c = this.map[x][y];        
//        if (this.map!=null) c = this.map[x][y];
        else {
             String smap = (map==null) ? "Mapa.getElement: this.map=null":"";
            String sborder = (checkBorders(x, y)) ? "Mapa.getElement: borders :" +x+","+y : "";
            System.out.println(smap+ "\n" + sborder);
        }
        return c;
    }
    
    public void setElement(char c, int x, int y){
//        if (this.map!=null && checkBorders(x, y)){
        if (this.map!=null){
            this.map[x][y] = c;
        } else {
            String smap = (map==null) ? "Mapa.setElement: this.map=null":"";
            String sborder = (!checkBorders(x, y)) ? "Mapa.setElement: borders :" +x+","+y : "";
            System.out.println(smap+ "\n" + sborder);
        }
      
    }
    
    
    //getters && setters
    public char[][] getMap() {
        return map;
    }

    public void setMap(char[][] map) {
        this.map = map;
    }

    public String getRutaMapa() {
        return rutaMapa;
    }

    public void setRutaMapa(String rutaMapa) {
        this.rutaMapa = rutaMapa;
    }

    public int getPellets() {
        return pellets;
    }

    public void setPellets(int pellets) {
        this.pellets = pellets;
    }

    public int getPowerPellets() {
        return powerPellets;
    }

    public void setPowerPellets(int powerPellets) {
        this.powerPellets = powerPellets;
    }
    
   

    private boolean checkBorders(int x, int y) {
        return (x>0 && x<PacmanUtils.COLUMNAS && y>0 && y<PacmanUtils.FILAS);
    }
    
     public static void main(String[] args){
        Mapa m = new Mapa();
        
        System.out.println("pellets:" +m.getPellets());
        System.out.println("power pellets:" +m.getPowerPellets());
        System.out.print(m.toString());
    }
}

